package ;

import flixel.FlxG;
import flixel.group.FlxTypedGroup;
import flixel.util.FlxPoint;
import openfl.Assets;

/**
 * ...
 * @author LF
 */
class TreeSegmentSlots extends FlxTypedGroup<ItemHolder>
{

	private var _item1:ItemHolder;
	private var _item2:ItemHolder;

	public function new(places:Array<FlxPoint>) 
	{
		super();
		
		/*for (xy in places)
		{
			add(new ItemHolder(xy.x, xy.y));
		}*/
		
		if (places.length < 2)
		{
			throw "Error";
		}
		_item1 = new ItemHolder(places[0].x, places[0].y);
		add(_item1);
		_item2 = new ItemHolder(places[1].x, places[1].y);
		add(_item2);
		
	}
	
	public function usingMouse():Bool
	{
		/*for (holder in members)
		{
			if (holder.overlapsPoint(FlxG.mouse) && holder.hasItem())
			{
				return true;
			}
		}
		return false;*/
		
		return (_item1.overlapsPoint(FlxG.mouse) && _item1.hasItem()) || (_item2.overlapsPoint(FlxG.mouse) && _item2.hasItem());
	}
	
	public function getClickedItem():Item
	{
		/*for (holder in members)
		{
			if (holder.overlapsPoint(FlxG.mouse))
			{
				return holder.getItem();
			}
		}*/
		
		if (_item1.overlapsPoint(FlxG.mouse))
		{
			return _item1.getItem();
		}
		else if (_item2.overlapsPoint(FlxG.mouse))
		{
			return _item2.getItem();
		}
		return null;
		
	}
	
	public function overlapsItem(I:Item):Bool
	{
		/*for (holder in members)
		{
			if (holder.overlaps(I, true) && !holder.hasItem())
			{
				return true;
			}
		}
		return false;*/
		
		return (_item1.overlaps(I, true) && !_item1.hasItem()) || (_item2.overlaps(I, true) && !_item2.hasItem());
	}
	
	public function setSlotItem(I:Item):Void
	{
		/*for (holder in members)
		{
			if (holder.overlaps(I, true) && !holder.hasItem())
			{
				holder.setItem(I);
			}
		}*/
		
		if (_item1.overlaps(I, true) && !_item1.hasItem())
		{
			_item1.setItem(I);
		}
		else if (_item2.overlaps(I, true) && !_item2.hasItem())
		{
			_item2.setItem(I);
		}
		
	}
	
	public function bothFull():Bool
	{
		return _item1.hasItem() && _item2.hasItem();
	}

	public function combine():String
	{
		// Xml describing all items and combinations
		var itemXml:Xml = Xml.parse(Assets.getText(AssetPaths.Items__xml));

		// The name of one of the items we're holding
		var item1Name:String = _item1.heldItemName();
		
		// The node corresponding to that item
		var item1Node:Xml = itemXml.firstElement().elementsNamed(item1Name).next();
		
		// The name of the other item we're holding
		var item2Name:String = _item2.heldItemName();

		// Check if there's a valid combination for these two items
		// by checking if the first item's node has a child with
		// the second item's name
		if (item1Node.elementsNamed(item2Name).hasNext())
		{
			// If it does, get that node
			var item2Node:Xml = item1Node.elementsNamed(item2Name).next();
			
			// Extract and return the name of the combined item
			return item2Node.get("com");
			
		}
		
		// If this wasn't a valid combination, return null
		return null;
		
	}
	
	public function consumeItems():Void {
		_item1.consumeItem();
		_item2.consumeItem();
	}
	
}